﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Pivotal.Discovery.Client;
using Microsoft.Extensions.Logging;
using System.Net.Http;
using System.Net;
using System.Diagnostics;
using System.Text;
using System.Net.Http.Headers;

namespace Core.Client1.Controllers
{
    [Route("/")]
    public class OrderController : Controller
    {
        private readonly DiscoveryHttpClientHandler _handler;
        private const string ProductUrl = "http://localhost:5555/product";

        public OrderController(IDiscoveryClient client, ILoggerFactory logFactory)
        {
            _handler = new DiscoveryHttpClientHandler(client);
        }
        // GET api/values
        [HttpGet]
        public string Get()
        {
            return "order";
        }
        [HttpGet("product")]
        public async Task<string> GoProductAsync()
        {
            var client = new HttpClient(_handler, false);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
            //var response =await  client.GetAsync(ProductUrl + "/1");
            var content = Newtonsoft.Json.JsonConvert.SerializeObject(new { value = "123" });
            var httpContent = new StringContent(content, Encoding.UTF8);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json") { CharSet = "utf-8" };
            var response = await client.PostAsync(ProductUrl, httpContent);
            string responseString = await response.Content.ReadAsStringAsync();
            return responseString;
        }
        // GET api/values/5

    }
}
